package com.yanqu.road.server.cmd.activity.common;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityRewardResult;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.common.CommonActivityModule;
import com.yanqu.road.server.manger.activity.CommonActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_COMMON_ACTIVITY_GET_REWARD, desc = "领取通用活动奖励")
public class CommonActivityGetRewardCmd extends AbstractCommand {

    private int activityId;
    private int conditionId;
    private ActivityRewardResult result;
    private boolean isNeedResp = true;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityGetRewardReqMsg reqMsg = CommonActivityProto.CommonActivityGetRewardReqMsg.parseFrom(packet.getBytes());
        conditionId = reqMsg.getConditionId();
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityConditionInfo conditionInfo = CommonActivityMgr.getCommonActivityConditionInfo(activityId, conditionId);
        if (ActivityHelper.COMMON_ACTIVITY_CONDITION_TYPE_USER != conditionInfo.getType() &&
                ActivityHelper.COMMON_ACTIVITY_CONDITION_TYPE_UNION != conditionInfo.getType() &&
                ActivityHelper.COMMON_ACTIVITY_CONDITION_TYPE_CROSS != conditionInfo.getType()) {
            ret = GameErrorCode.E_COMMON_ACTIVITY_GET_REWARD_TYPE_ERROR;
            return;
        }
        result = player.getModule(CommonActivityModule.class).getRankReward(activityId, conditionId, false, 0);
        if(ActivityHelper.COMMON_ACTIVITY_CONDITION_TYPE_CROSS == conditionInfo.getType() && 0 == result.getRet()){
            isNeedResp = false;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(isNeedResp) {
            CommonActivityProto.CommonActivityGetRewardRespMsg.Builder respMsg = CommonActivityProto.CommonActivityGetRewardRespMsg.newBuilder();
            respMsg.setRet(ret);
            if (0 == ret) {
                if (null != result) {
                    respMsg.setRet(result.getRet());
                    respMsg.setReward(result.getReward());
                }
            }
            player.sendPacket(Protocol.U_COMMON_ACTIVITY_GET_REWARD, respMsg);
        }
    }
}
